package com.hmall.order.service.impl;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.api.AddressFeignClient;
import com.hmall.common.api.ItemFeignClient;
import com.hmall.common.pojo.Address;
import com.hmall.common.pojo.Item;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderDto;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.service.IOrderService;
import com.hmall.order.utils.UserHolder;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets;
import java.util.Date;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ItemFeignClient itemFeignClient;
    @Autowired
    private AddressFeignClient addressFeignClient;
    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public Long create(OrderDto orderDto) {
        Long itemId = orderDto.getItemId();
        Item item = itemFeignClient.getById(itemId);
        Long addressId = orderDto.getAddressId();
        Address address = addressFeignClient.findAddressById(addressId);
        Long price = item.getPrice();
        int num = orderDto.getNum();
        Long totalFree=price*num;
        Order order = new Order();
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setUserId(UserHolder.getUser());
        order.setStatus(1);
        order.setPaymentType(orderDto.getPaymentType());
        order.setTotalFee(totalFree);
        long orderId = IdWorker.getId();
        order.setId(orderId);
        orderMapper.insert(order);

        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(orderId);
        orderDetail.setItemId(orderDto.getItemId());
        orderDetail.setNum(orderDto.getNum());
        orderDetail.setCreateTime(new Date());
        orderDetail.setUpdateTime(new Date());
        orderDetail.setImage(item.getImage());
        orderDetail.setName(item.getName());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setPrice(item.getPrice());
        //保存订单详情
        orderDetailMapper.insert(orderDetail);

        itemFeignClient.update(itemId,num);
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics.setCreateTime(new Date());
        orderLogistics.setUpdateTime(new Date());
        orderLogistics.setCity(address.getCity());
        orderLogistics.setProvince(address.getProvince());
        orderLogistics.setContact(address.getContact());
        orderLogistics.setPhone(address.getMobile());
        orderLogistics.setStreet(address.getStreet());
        orderLogistics.setTown(address.getTown());
        orderLogistics.setOrderId(orderId);
        orderLogisticsMapper.insert(orderLogistics);

        //扣减库存
        boolean updateStock = itemFeignClient.update(orderDto.getItemId(), orderDto.getNum());
        if (!updateStock) {
            throw new RuntimeException("库存不足");
        }
        //延迟队列，定时关闭订单
        Message message = MessageBuilder
                .withBody(String.valueOf(orderId).getBytes(StandardCharsets.UTF_8))
                .setHeader("x-delay", 1000 * 60 * 30)
                .build();
        //发送消息到延迟队列
        rabbitTemplate.convertAndSend("delayExchange", "delayKey", message);
        return orderId;
    }
}
